package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.bean.MyBean;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.CartBuyModel;
import cn.edu.neusoft.wuguangsheng.market.model.MyModel;

public class CartBuyActivity extends BaseActivity {

    Spinner spinner;
    ArrayAdapter<String> arrayAdapter;
    String[] str = { "7:00-8:00", "8:00-9:00","9:00-10:00","10:00-11:00","11:00-12:00",
            "12:00-13:00","13:00-14:00","14:00-15:00","15:00-16:00","16:00-17:00","17:00-18:00",
            "18:00-19:00","19:00-20:00","20:00-21:00","21:00-22:00"};

    String mrid;
    ImageView cart_buy_ret;
    EditText cart_buy_time;
    Button cart_buy_buy;
    String shsj;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_cart_buy);
       // Toast.makeText(CartBuyActivity.this,mrid,Toast.LENGTH_SHORT).show();
        cart_buy_ret=(ImageView)findViewById(R.id.cart_buy_ret);


        cart_buy_buy=(Button)findViewById(R.id.cart_buy_buy);

        spinner = (Spinner) findViewById(R.id.spi);
        arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_spinner_item, str);

        // spinner.setPrompt("测试");
        spinner.setPromptId(R.id.spi);

        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);



        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new ItemClick());

//        spinner.setAdapter(arrayAdapter);
//        spinner.setOnItemSelectedListener(new ItemClick());
    }


    private class ItemClick implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
                                   long arg3) {
         shsj=(String) parent.getItemAtPosition(arg2);
//            Toast.makeText(CartBuyActivity.this,
//                    "点击的是" + parent.getItemAtPosition(arg2), Toast.LENGTH_SHORT)
//                    .show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    }

    @Override
    void initEvent() {
cart_buy_buy.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

//        if(suggesttime.equals("")){
//            Toast.makeText(CartBuyActivity.this,"输入信息不能为空",Toast.LENGTH_SHORT).show();
//        }
//        else{
            String uid=getid();
            mrid=getIntent().getStringExtra("mrid");
            CartBuyModel cartBuyModel=new CartBuyModel();
            cartBuyModel.cartBuy(uid,shsj,mrid,tListener);

    }
});
        cart_buy_ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Override
    void initDate() {

    }

    TListener<RegisterBean> tListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
   String success=registerBean.getSuccess();
            if(success.equals("1")){
                Toast.makeText(CartBuyActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else if(success.equals("2")){
                Toast.makeText(CartBuyActivity.this,"收货人为空，请填写收货人信息",Toast.LENGTH_SHORT).show();
                String uid=getid();
                MyModel myModel=new MyModel();
                myModel.getMyinfo(uid,cListener);
            }
            else{
                Toast.makeText(CartBuyActivity.this,"下单失败",Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<MyBean> cListener=new TListener<MyBean>() {
        @Override
        public void onResponse(MyBean myBean) {
            String uname=myBean.getUname();
            String phone=myBean.getPhone();
            String pic=myBean.getPic();
            String shname=myBean.getShname();
            String address=myBean.getAddress();
            // Toast.makeText(BuyActivity.this,address,Toast.LENGTH_SHORT).show();
            Intent intentq=new Intent(CartBuyActivity.this,MySetActivity.class);
            intentq.putExtra("shname",shname);
            intentq.putExtra("phone",phone);
            intentq.putExtra("address",address);
            startActivity(intentq);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(CartBuyActivity.this,"???", Toast.LENGTH_SHORT).show();
        }
    };
}
